package exp1_1_z09315222.cslg.cn.exp1_2_z09315222;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class KeyDownDemo extends AppCompatActivity {

    private TextView textView;

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key_down_demo);

        textView = (TextView) findViewById(R.id.tv1);
        button = (Button) findViewById(R.id.button1);

//        //内部类的形式实现监听器
//        MyClickListener clickListener = new MyClickListener();
//        button.setOnClickListener(clickListener);

        //以匿名内部类的形式实现监听器
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(KeyDownDemo.this, "你点击了Button按钮", Toast.LENGTH_SHORT).show();
            }
        });
    }
//
//    //内部类的形式实现监听器
//    class MyClickListener implements View.OnClickListener{
//
//        @Override
//        public void onClick(View v) {
//            Toast.makeText(KeyDownDemo.this, "你点击了button按钮", Toast.LENGTH_SHORT).show();
//        }
//    }

    /**
     * 检测到键盘按键的时候进行调用
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_DOWN){
            textView.append(String.valueOf(event.getDisplayLabel()));
        }

        return super.onKeyDown(keyCode, event);
    }
}
